
const db = require("../utils/db");
const util = require("../utils/util")

async function add_comment(user_id, content, level, post_id, father_id, reply_id) {
    var sql = "Insert into comment(user_id, content, level, post_id, father_id, reply_id, comment_time)\
                values(?,?,?,?,?,?,?)";
    var args = [user_id, content, level, post_id, father_id, reply_id, util.datestring(new Date())];
    return (await db.query(sql, args)).insertId;
}

async function del_comment(comment_id) {
    var sql = `delete from comment where comment.comment_id = ${comment_id}`;
    await db.query(sql);
}

async function get_user_id(comment_id) {
    // 从comment_id获取user_id，用以身份验证
    var sql = `SELECT user_id from comment where comment_id = ${comment_id}`
    return (await db.query(sql))[0].comment_id;
}

async function get_poster_id(comment_id) {
    // 从comment_id获取其所在post主的user_id，用以身份验证
    var sql = `SELECT post_id from comment where comment_id = ${comment_id}`
    var post_id = (await db.query(sql))[0].comment_id;
    sql = `SELECT user_id from post where post_id = ${post_id}`
    return (await db.query(sql))[0].post_id;
}

module.exports = {
    add_comment: add_comment,
    del_comment: del_comment,
    get_user_id: get_user_id,
    get_poster_id: get_poster_id,
}
